from OpenGL.arrays import vbo
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from numpy import array
from collections import namedtuple
import sys, math, os

##Point = namedtuple('POINT', ['x', 'y'], verbose=True)
Point = namedtuple('POINT3', ['x', 'y','z'], verbose=True)

p1 = Point(1,0,0) # 1
p2 = Point(2,2,0) # 2
p3 = Point(3,0,0) # 3 
p4 = Point(4,2,0) # 4
p5 = Point(5,0,0) # 5
p6 = Point(6,2,0) # 6
p7 = Point(5,4,0) # 7
p8 = Point(7,4,0) # 8
p9 = Point(8,2,0) # 9
p10 = Point(9,4,0) # 10
p11 = Point(10,2,0)# 11
p12 = Point(9,0,0) # 12
p13 = Point(11,0,0)# 13

origin, e1, e2, e3 = Point(0,0,0), Point(1,0,0), Point(0,1,0), Point(0,0,1)

##points = [1,2,3,4,5,4,6,7,6,8,9,10,9,11,12,13]
points = [p1,p2,p3,p4,p5,p4,p6,p7,p6,p8,p9,p10,p9,p11,p12,p13,
          origin, e2,
          origin, e3
          ]

points2 = [origin, e1,
           origin, e2,
           origin, e3
           ]

vbo = vbo.VBO(array(points, 'f')) # vbo an Graka schicken
##vbo2 = vbo.VBO(array(points2, 'f'))

def init(width, height):
   """ Initialize an OpenGL windown and rendering context"""
   glClearColor(1, 1, 1, 1)         # background color
   glMatrixMode(GL_PROJECTION)      # switch to projection matrix
   glLoadIdentity()                 # set to 1
##   gluOrtho2D(-1, 15,               # links, rechts clipping planes
##              -1, 15)               # unten, oben  clipping planes
   gluPerspective(45.,              # oeffnungswinkel fovy
                  width/height,  # aspect ratio
                  0.1,           # near
                  100.           # far
                  )                  
   glMatrixMode(GL_MODELVIEW)       # switch to modelview matrix
   
   
def display():
   """Rendering einstellungen"""
   glClear(GL_COLOR_BUFFER_BIT,
           GL_DEPTH_BUFFER_BIT
           ) # resettet angegebenen Buffer
   glLoadIdentity()              # Matrix resetten
   gluLookAt(5, 0, 30,           # eye                
             5, 0, 0,           # center
             0, 1, 0             # up
             )
   glColor3f(0,0,0)
   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
   # Render Vertex Buffer Object
   # Rotiert wird gegen den Uhrzeigersinn CCW
   glRotate(45,                  # Winkel
            0, 1, 0              # Achse
            )
   # Axis
##   glColor(1,0,0)
##   vbo2.bind()
##   # Array von Punkten definieren
##   glVertexPointer(3,      # Anzahl Koordinaten pro Vertex (default 4)
##                   GL_FLOAT, # Datentyp der Koordinaten (default GL_FLOAT immer gut)
##                   0,      # stride (Byte-Abstand zum naechsten Anfangswert (default 0)
##                   vbo2    # Pointer auf erstes Element erste Koordinate
##                   )
##   glEnableClientState(GL_VERTEX_ARRAY)
##   glDrawArrays(GL_LINES, 0, 6)
##   vbo2.unbind()
##   glDisableClientState(GL_VERTEX_ARRAY)
   
   # Triangles
   vbo.bind()
   glVertexPointerf(vbo)
   glEnableClientState(GL_VERTEX_ARRAY)
   glDrawArrays(GL_TRIANGLE_STRIP, 0, 16) # !! Anzahl Arrayelemente mit doppelten Kanten beachten
   glColor(1,0,0)
   glDrawArrays(GL_LINES, 17, 6)
   vbo.unbind()
   glDisableClientState(GL_VERTEX_ARRAY)
   
   # Flush commands  
   glFlush()  
   
 
def reshape():
   pass
   
def main():
   width, height = 500, 500
   glutInit(sys.argv)
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB )   
   glutInitWindowSize(width, height)
   glutCreateWindow("Test OPENGL Fensterchen")
   glutDisplayFunc(display)     #register display function

   
   # Hack for Mac OS X
#   cwd = os.getcwd()
#   glutInit(sys.argv)
#   os.chdir(cwd)

   init(width, height)#initialize OpenGL state and opengl context
   glutMainLoop()   #start even processing

if __name__ == "__main__":
   main()
